package com.example.assisteddriving.dao;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;


import com.example.assisteddriving.bean.Drivelog_;
import com.example.assisteddriving.bean.Onedrivelog;
import com.example.assisteddriving.bean.Tiwenlog;
import com.example.assisteddriving.bean.Xinlvlog;
import com.example.assisteddriving.bean.Xueyanglog;
import com.example.assisteddriving.util.DrivelogSQLiteOpenHelper;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

public class DrivelogDao {
    private static final String DB = "drive.db"; //数据库名
    private static final String DB_TABLE = "drivelog"; //表名

    private DrivelogSQLiteOpenHelper dbOpenHelper;  //数据库帮助类对象
    public DrivelogDao(Context context){
        dbOpenHelper = new DrivelogSQLiteOpenHelper(context,DB,null,1);
    }

    public void insert(Onedrivelog onedrivelog){
        SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
        if(db.isOpen()){
            String sql="insert into drivelog values(null,?,?,?,?)";
            db.execSQL(sql,new Object[]{onedrivelog.getLog(),onedrivelog.getTired(),onedrivelog.getSleep(),onedrivelog.getTime()});
            db.close();
        }
    }
    public void insertxinlv(String xl,String tw,String xy,String sj){
        SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
        if(db.isOpen()){
            String sql="insert into xinlv values(null,?,?,?,?)";
            db.execSQL(sql,new Object[]{xl,tw,xy,sj});
//            Log.v("asd","baocun"+xl+tw+xy+sj);
            db.close();
        }
    }

    public void delete(String log){
        SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
        if(db.isOpen()){
            String sql="delete from drivelog where log=?";
            db.execSQL(sql,new String[]{log});
            db.close();
        }
    }

    public List<Drivelog_> getlog(String log){
        SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
        if(db.isOpen()){
            String sql="select tired,sleep,time from drivelog where log=?";
            Cursor cursor=db.rawQuery(sql,new String[]{log});
            if(cursor!=null&&cursor.getCount()>0){
                List<Drivelog_> list= new ArrayList<>();
                while (cursor.moveToNext()){
                    Drivelog_ drivelog_=new Drivelog_();
                    drivelog_.setTired(cursor.getString(0));
                    drivelog_.setSleep(cursor.getString(1));
                    drivelog_.setTime(cursor.getLong(2));
                    list.add(drivelog_);
                }
                db.close();
                return list;
            }
        }
        db.close();
        return null;
    }

    public List<Xinlvlog> getxl(){
        SQLiteDatabase db =dbOpenHelper.getWritableDatabase();
        if(db.isOpen()){
            String sql="select xinlv,tiwen,xueyang,time from xinlv";
            Cursor cursor=db.rawQuery(sql,null);
            if (cursor!=null&&cursor.getCount()>0){
                List<Xinlvlog> list=new ArrayList<>();
                while (cursor.moveToNext()){
                    Xinlvlog xinlvlog=new Xinlvlog();
                    xinlvlog.setXinlv(cursor.getString(0));
                    xinlvlog.setTiwen(cursor.getString(1));
                    xinlvlog.setXueyang(cursor.getString(2));
                    xinlvlog.setTime(cursor.getString(3));
                    list.add(xinlvlog);
                }
                db.close();
                return  list;
            }
        }
        db.close();
        return null;
    }


    public List<Tiwenlog> gettw(){
        SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
        if(db.isOpen()){
            String sql="select tiwen,time from xinlv";
            Cursor cursor=db.rawQuery(sql,null);
            if(cursor!=null&&cursor.getCount()>0){
                List<Tiwenlog> list= new ArrayList<>();
                while (cursor.moveToNext()){
                    Tiwenlog tiwenlog=new Tiwenlog();
                    tiwenlog.setTiwen(cursor.getString(0));
                    tiwenlog.setTime(cursor.getString(1));
                    list.add(tiwenlog);
                }
                db.close();
                return list;
            }
        }
        db.close();
        return null;
    }
    public List<Xueyanglog> getxy(){
        SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
        if(db.isOpen()){
            String sql="select xueyang,time from xinlv";
            Cursor cursor=db.rawQuery(sql,null);
            if(cursor!=null&&cursor.getCount()>0){
                List<Xueyanglog> list= new ArrayList<>();
                while (cursor.moveToNext()){
                    Xueyanglog xueyanglog=new Xueyanglog();
                    xueyanglog.setXueyang(cursor.getString(0));
                    xueyanglog.setTime(cursor.getString(1));
                    list.add(xueyanglog);
                }
                db.close();
                return list;
            }
        }
        db.close();
        return null;
    }

    /*public String queryIp(String friendName){
        SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
        if(db.isOpen()){
            String sql="select friendIp from friends where friendName=?";
            Cursor cursor=db.rawQuery(sql,new String[]{friendName});
            if(cursor!=null&&cursor.getCount()>0){
                List<String> list=new ArrayList<String>();
                String friendIp;
                while (cursor.moveToNext()){
                    friendIp=cursor.getString(0);
                }
                db.close();
                return friendIp;
            }
        }
        db.close();
        return null;
    }*/



}
